const { db, genid } = require("../db/index");
const { v4: uuidv4 } = require("uuid");

// 增
exports.add = async (req, res) => {
  const blog = req.body;
  let id = genid.NextId();
  let create_time = new Date().getTime();

  blog.id = id;
  blog.create_time = create_time;

  const insert_sql = "insert into blog set ?";

  let { err, results } = await db.async.query(insert_sql, blog);
  if (err == null) {
    res.json({
      code: 200,
      msg: "添加成功",
    });
  } else {
    res.json({
      code: 500,
      msg: "添加失败",
    });
  }
};
// 删
exports.delete = async (req, res) => {
  let id = req.query.id;
  const delete_sql = "delete from blog where id = ?";
  let { err, results } = await db.async.query(delete_sql, [id]);
  if (err == null) {
    res.json({
      code: 200,
      msg: "删除成功",
    });
  } else {
    res.json({
      code: 500,
      msg: "删除失败",
    });
  }
};

// 改
exports.update = async (req, res) => {
  const blog = req.body;
  const update_sql = "update blog set ? where id = ?";
  let { err, results } = await db.async.query(update_sql, [blog, blog.id]);
  if (err == null) {
    res.json({
      code: 200,
      msg: "修改成功",
    });
  } else {
    res.json({
      code: 500,
      msg: "修改失败",
    });
  }
};

// 查
exports.search = async (req, res) => {
  /**
   * keyword 关键字
   * category_id 分类编号
   *
   * 分页：
   * page 页码
   * page_size 分页大小
   */
  let { keyword, category_id, page, page_size } = req.query;
  page = page == null ? 1 : page;
  page_size = page_size == null ? 10 : parseInt(page_size);
  //   console.log(typeof(page_size))
  category_id = category_id == null ? 0 : category_id;
  keyword = keyword == null ? "" : keyword;

  const whereSql = [];
  const params = [];
  if (category_id != 0) {
    whereSql.push(" category_id = ? ");
    params.push(category_id);
  }

  if (keyword != "") {
    whereSql.push(" (title like ? or content like ?) ");
    params.push("%" + keyword + "%");
    params.push("%" + keyword + "%");
  }

  let whereSqlStr = "";
  if (whereSql.length > 0) {
    whereSqlStr = " where " + whereSql.join(" and ");
  }

  // 分页数据
  let searchSql =
    " select id, category_id, title, substr(content, 1, 80) as content, create_time from blog " +
    whereSqlStr +
    " order by create_time desc limit ?, ? ";
  //   console.log(searchSql)
  let searchSqlParams = params.concat([(page - 1) * page_size, page_size]);

  //   console.log(searchSqlParams)

  // 数据总数
  let searchCountSql = " select count(*) as count from blog " + whereSqlStr;
  let searchCountParams = params;

  let searchResult = await db.async.query(searchSql, searchSqlParams);
  //   console.log(searchResult)
  let countResult = await db.async.query(searchCountSql, searchCountParams);
  //   console.log(countResult.results[0]);

  if (searchResult.err == null && countResult.err == null) {
    res.json({
      count: 200,
      msg: "查询成功",
      data: {
        keyword,
        category_id,
        page,
        page_size,
        results: searchResult.results,
        count: countResult.results[0].count,
      },
    });
  } else {
    res.json({
      code: 500,
      msg: "查询失败",
    });
  }
};

// 获取单篇博客的详细内容
exports.detail = async (req, res) => {
  let { id } = req.query;
  const sql = "select * from blog where id = ?";
  let { err, results } = await db.async.query(sql, [id]);
  if (err == null) {
    res.json({
      code: 200,
      msg: "查询成功",
      results,
    });
  } else {
    res.json({
      code: 500,
      msg: "查询失败",
    });
  }
};
